package com.yyys.web.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by zhengsenwen on 2017/8/3.
 */
public class DateUtils {


    /**
     * 获取该展示早餐、午餐还是晚餐
     *
     * @return
     */
    public static String getBreakfastLunchOrDinner() {
        Date now = stringToDate(getCurrentTime());

        if (now.after(stringToDate("00:00:00")) && now.before(stringToDate("08:29:59"))) {
            return "早餐";
        } else if (now.after(stringToDate("08:30:00")) && now.before(stringToDate("13:29:59"))) {
            return "午餐";
        } else if (now.after(stringToDate("13:30:00")) && now.before(stringToDate("23:59:59"))) {
            return "晚餐";
        } else {
            return null;
        }
    }

    /**
     * 获取当前时间距离最近的用餐时间
     *
     * @return
     */
    public static String getMealTime() {
        Date now = stringToDate(getCurrentTime());

        if (now.after(stringToDate("00:00:00")) && now.before(stringToDate("08:29:59"))) {
            return "06:30:00-08:00:00";
        } else if (now.after(stringToDate("08:30:00")) && now.before(stringToDate("13:29:59"))) {
            return "11:30:00-13:00:00";
        } else if (now.after(stringToDate("13:30:00")) && now.before(stringToDate("23:59:59"))) {
            return "17:00:00-19:00:00";
        } else {
            return null;
        }
    }


    /**
     * 获取header部分展示的今天几号 星期几
     */

    public static String getTodayInfo() {
        String today = DateUtils.getCurrentDay();
        String weekday = DateUtils.getWeekByDateStr(today);
        return today + "    " + weekday;
    }


    /**
     * String转Date  "00:00:00" —— date.
     *
     *
     * @param time
     * @return
     */
    public static Date stringToDate(String time) {
        DateFormat format = new SimpleDateFormat("HH:mm:ss");//日期格式
        Date date = null;
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    /**
     * 判断time是否在from，to之内
     *
     * @param time 指定日期
     * @param from 开始日期
     * @param to   结束日期
     * @return
     */
    public static boolean belongCalendar(Date time, Date from, Date to) {
        Calendar date = Calendar.getInstance();
        date.setTime(time);

        Calendar after = Calendar.getInstance();
        after.setTime(from);

        Calendar before = Calendar.getInstance();
        before.setTime(to);

        if (date.after(after) && date.before(before)) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 获取前N天或者后N天的日期 格式：2017-09-07
     * @return
     */
    public static String getLastDay(int number){
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH,number);
        date = calendar.getTime();
        return simpleDateFormat.format(date);
    }

    /**
     * 获取当前系统时间
     *
     * @return
     */

    public static String getCurrentTime() {
        long time = Calendar.getInstance().getTimeInMillis();

        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式
        String date = df.format(time);// new Date()为获取当前系统时间，也可使用当前时间戳

        return date;
    }



    /**
     * 获取当前系统日期
     *
     * @return
     */
    public static String getCurrentDay() {
        long time = Calendar.getInstance().getTimeInMillis();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        return df.format(time);// new Date()为获取当前系统时间，也可使用当前时间戳
    }

    /**
     * <pre>
     * 根据指定的日期字符串获取星期几
     * </pre>
     *
     * @param strDate 指定的日期字符串(yyyy-MM-dd 或 yyyy/MM/dd)
     * @return week
     */
    public static String getWeekByDateStr(String strDate) {

        int year = Integer.parseInt(strDate.substring(0, 4));
        int month = Integer.parseInt(strDate.substring(5, 7));
        int day = Integer.parseInt(strDate.substring(8, 10));

        Calendar c = Calendar.getInstance();

        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, day);

        String week = "";
        int weekIndex = c.get(Calendar.DAY_OF_WEEK);

        switch (weekIndex) {
            case 1:
                week = "星期日";
                break;
            case 2:
                week = "星期一";
                break;
            case 3:
                week = "星期二";
                break;
            case 4:
                week = "星期三";
                break;
            case 5:
                week = "星期四";
                break;
            case 6:
                week = "星期五";
                break;
            case 7:
                week = "星期六";
                break;
        }
        return week;
    }
}
